package com.test.videoplayer.activity.play;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.dueeeke.videocontroller.StandardVideoController;
import com.dueeeke.videoplayer.player.VideoView;
import com.dueeeke.videoplayer.player.VideoViewManager;
import com.test.videoplayer.R;

import java.util.Objects;

/**
 * 多开
 */
public class ParallelPlayTestActivity extends AppCompatActivity {

    private static final String VOD_URL = "http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4";
    private static final String VOD_URL1 = "http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4";
    private static final String VOD_URL2 = "http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4";
    private AudioManager mAudioManager;
    private ImageView mMute1,mMute2;
    private boolean isMute = false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parallel_play_test);
        ActionBar actionBar = getSupportActionBar();
        VideoView player1 = findViewById(R.id.player_1);
        VideoView player2 = findViewById(R.id.player_2);
        //TODO->play1播放器
        player1.setUrl(VOD_URL);
        //这两项必须设置
        player1.setEnableAudioFocus(false);
        player1.setEnableParallelPlay(true);
        StandardVideoController controller = new StandardVideoController(this);
        controller.setLive();
        controller.setTitle("云A7771");
        player1.setVideoController(controller);
        player1.start();
        player1.setMute(true);

        //TODO->play2播放器
        player2.setUrl(VOD_URL);
        //这两项必须设置
        player2.setEnableAudioFocus(false);
        player2.setEnableParallelPlay(true);
        StandardVideoController controller2 = new StandardVideoController(this);
        controller2.setLive();
        controller2.setTitle("云A7771");
        player2.setVideoController(controller2);
        player2.start();
        player2.setMute(true);
      //  playUrlPlayer1(player1,VOD_URL1,"player1");
     //   playUrlPlayer1(player2,VOD_URL2,"player2");

    }
    /**
     * 播放多个直播直播视频源
     * @param player 播放器
     * @param VOD_URL 直播源地址
     */
    private void playUrlPlayer1(VideoView player, String VOD_URL,String playerStr) {
        player.setUrl(VOD_URL);
        //这两项必须设置
        player.setEnableAudioFocus(false);
        player.setEnableParallelPlay(true);
        StandardVideoController controller = new StandardVideoController(this);
        controller.setLive();
        controller.setTitle("云A7771");
        player.setVideoController(controller);
        player.start();
        AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        if("player1".equals(playerStr)){
            Objects.requireNonNull(mAudioManager).setStreamVolume(AudioManager.STREAM_MUSIC, (int) 0f, 0);
        }else if("player2".equals(playerStr)){
            Objects.requireNonNull(mAudioManager).setStreamVolume(AudioManager.STREAM_MUSIC, (int) 0f, 0);
        }
    }
    /**
     * 播放多个直播直播视频源
     * @param player 播放器
     * @param VOD_URL 直播源地址
     */
    private void playUrlPlayer2(VideoView player, String VOD_URL) {
        player.setUrl(VOD_URL);
        //这两项必须设置
        player.setEnableAudioFocus(false);
        player.setEnableParallelPlay(true);
        StandardVideoController controller = new StandardVideoController(this);
        controller.setLive();
        controller.setTitle("云A7771");
        player.setVideoController(controller);
        player.start();
    }


    @Override
    protected void onPause() {
        Log.e("Tag","11111111111111");
        super.onPause();
        VideoViewManager.instance().pause();
    }

    @Override
    protected void onResume() {
        Log.e("Tag","22222222222");
        super.onResume();
        VideoViewManager.instance().resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("Tag","33333333333333");
        VideoViewManager.instance().release();
    }

    @Override
    public void onBackPressed() {
        if (VideoViewManager.instance().onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
}
